﻿using System;
using System.Reflection;
using System.Web;
using XProject.DataVisit.Handler.View;
using XProject.Commons.BaseUtil;
using log4net;

namespace XProject.DataVisit.Handler
{
    public class ViewManager : IViewManager
    {
        private IJsonResponse _response = new JsonResponse();
        private JsonRequest _request;
        private AbstractView _view;
        private bool _foundView = false;
        private static readonly string _defaultNamespace = "XProject.DataVisit.Handler.View";
        private MethodInfo _methodInfo;
        private static readonly ILog _log = LogManager.GetLogger(typeof(ViewManager));
        private HttpContext _context;

        public ViewManager(HttpContext context)
        {
            try
            {
                Log.Debug("Context Id :" + context);
                _context = context;
                Log.Debug("Session Id :" + context.Session.SessionID);
                _request = new JsonRequest(context);
                string strUrl = context.Request.RawUrl;
                int aLength = context.Request.ApplicationPath.Length;
                if (aLength > 1)
                {
                    strUrl= strUrl.Remove(0, aLength);
                }
                _view = GetView(strUrl);
                
            }
            catch (Exception e)
            {
                Log.Error(e);
                JsonResponse.AddMsg(e.ToString(), true);
            }
        }

        public ViewManager(string url)
        {
            _view = GetView(url);
        }

        public IJsonResponse JsonResponse
        {
            get
            {
                return _response;
            }
        }

        public IJsonRequest JsonRequest
        {
            get
            {
                return _request;
            }
        }

        public ILog Log
        {
            get
            {
                return _log;
            }
        }

        private AbstractView GetView(string url)
        {
            AbstractView view = null;
            try
            {
                string className = _defaultNamespace + StringUtils.UrlToClassName(url);
                string methodName = StringUtils.UrlToMethodName(url);
                //创建View实例
                Log.Debug("类名称 :" + className);
                Log.Debug("方法名称 :" + methodName);
                view = Assembly.GetAssembly(typeof(ViewManager))
                    .CreateInstance(className, true) as AbstractView;
                if (view == null) throw new Exception("没有找到对应的路径.");
                //获得请求实例的方法
                _methodInfo = view.GetType().GetMethod(methodName);
                //view.JsonRequest = new JsonRequest();
                view.Context = _context;
                view.JsonRequest = _request;
                view.JsonResponse = _response;
                view.Log = LogManager.GetLogger(view.GetType());
                _foundView = true;
            }
            catch (Exception e)
            {
                Log.Error(e);
                JsonResponse.AddMsg(e.ToString(), true);
                _foundView = false;
            }
            return view;
        }

        public bool FoundView
        {
            get
            {
                return _foundView;
            }
        }

        public void Execute()
        {
            try
            {
                try
                {
                    _view.init();
                }
                catch (Exception e) 
                {
                    Log.Error(e);
                    _view.Initalized = false;
                    JsonResponse.AddMsg(e.ToString(), true);
                }
                _methodInfo.Invoke(_view, new object[] { });
            }
            catch (Exception e)
            {
                Log.Error(e);
                JsonResponse.AddMsg(e.ToString(), true);
            }
            finally
            {
                try
                {
                    _view.Dispose();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    JsonResponse.AddMsg(e.ToString(), true);
                }
                HttpResponse response = _context.Response;
                response.Write(_response.GetResponseString());
                response.Flush();
            }
        }
    }
}
